package org.zero.concurrency.demo.example.immutable;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.zero.concurrency.demo.annotations.ThreadSafe;

import java.util.Collections;
import java.util.Map;

/**
 * @program: concurrency-demo
 * @description:
 * @author: 01
 * @create: 2018-10-17 10:37
 **/
@Slf4j
@ThreadSafe
public class ImmutableExample2 {

    private static Map<Integer, Integer> map = Maps.newHashMap();

    static {
        map.put(1, 2);
        map.put(3, 4);
        map.put(5, 6);
        map = Collections.unmodifiableMap(map);
    }

    public static void main(String[] args) {
        // 此时map就是不可变对象了，修改会报错
        map.put(1, 3);
        log.info("{}", map.get(1));
    }
}
